/*
 * Powered By xTOOLs
 * Dev by DuRisk@pactera.com
 * Copyright © 2009 - 2018 pactera.com. All rights reserved.
 *
 * Generated at 2018年01月11日 16:25:26
 *
 */
package com.pactera.jep.service.sys.web.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.pactera.jep.autoconfigure.web.controller.BaseRestController;
import com.pactera.jep.core.exception.ServiceException;
import com.pactera.jep.orm.Filter;
import com.pactera.jep.orm.Order;
import com.pactera.jep.orm.Page;
import com.pactera.jep.sys.code.MetaQueryStatus;
import com.pactera.jep.sys.code.MetaQueryType;
import com.pactera.jep.sys.model.DataProvider;
import com.pactera.jep.sys.service.DataProviderService;
import com.pactera.jep.web.code.WebCode;
import com.pactera.jep.web.entity.NameValuePair;
import com.pactera.jep.web.util.NVUtils;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value="数据提供器管理")
@RestController
@RequestMapping("/dataProvider")
public class DataProviderRestController extends BaseRestController<DataProvider>{
    private static Logger logger = LoggerFactory.getLogger(DataProviderRestController.class);

	@Autowired
	private DataProviderService	dataProviderService;

	
	/**
	 * 为列表页面做数据准备
	 */
	@Override
	protected void prepareQuery(@ApiIgnore @RequestHeader("x-user-token") String token
            , Page<DataProvider> page, List<Filter> filters, List<Order> orders, HttpServletRequest request) {

		dataProviderService.query(page, filters, orders);
	}
	
	/**
	 * 新增页面
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/sync")
	public String sync(Model model){
		return "sys/dataProviderSync";
	}
	
	/**
	 * 编辑页面
	 * @param queryId
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/edit/{queryId}")
	public String edit(@PathVariable String queryId, Model model){
		
		DataProvider dataProvider = dataProviderService.get(queryId);
		model.addAttribute("dataProvider", dataProvider);
		model.addAttribute("opt", "edit");
		
		return "sys/dataProviderEdit";
	}
	
	/**
	 * 查看页面
	 * @param queryId
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/view/{queryId}")
	public String view(@PathVariable String queryId, Model model) {
		
		DataProvider dataProvider = dataProviderService.get(queryId);
		dataProvider.setQueryType(MetaQueryType.getLabel(dataProvider.getQueryType()));
		dataProvider.setStatus(MetaQueryStatus.getLabel(dataProvider.getStatus()));
		model.addAttribute("dataProvider", dataProvider);
		return "sys/dataProviderView";
	}
	
	/**
	 * 保存
	 * @param
	 * @return
	 */
	@PostMapping(value = "/save")
	public Map<String, Object> save(@RequestBody String dataProviderJson) {
		Map<String, Object> tips = new HashMap<String, Object>();
		try 
		{
			List<DataProvider> data = JSON.parseArray(dataProviderJson, DataProvider.class);
			dataProviderService.saveDataProvider(data);
			tips.put(WebCode.SUCCESS, true);
			tips.put(WebCode.MSG, WebCode.SUCCESS_SAVE);
		}
		catch (ServiceException e) 
		{
			logger.info(e.getMessage());
			tips.put(WebCode.SUCCESS, false);
			tips.put(WebCode.MSG, WebCode.FAILURE_SAVE);
		}
		return tips;
	}
	
	/**
	 * 删除
	 * @param queryId
	 * @return
	 */
	@RequestMapping(value = "/remove/{queryId}")
	@ResponseBody
	public Map<String, Object> remove(@PathVariable String queryId){
		
		Map<String, Object> tips = new HashMap<String, Object>();
		
		try 
		{
			dataProviderService.deleteByPrimaryKey(queryId);
			tips.put(WebCode.SUCCESS, true);
			tips.put(WebCode.MSG, WebCode.SUCCESS_DELETE);
		} 
		catch (ServiceException e) 
		{
			logger.info(e.getMessage());
			tips.put(WebCode.SUCCESS, false);
			tips.put(WebCode.MSG, WebCode.FAILURE_DELETE);
		}
		
		return tips;
	}
	
	/**
	 * 获取所有的数据提供程序
	 * @return
	 */
	@RequestMapping("/getDataProvider")
	@ResponseBody
	public List<NameValuePair> getDataProvider(){
		List<DataProvider> list = dataProviderService.selectAll();
		return NVUtils.convert(list, list.size(), "queryId", "queryName");
	}
	
}